# Maintainer: Giancarlo Razzolini <grazzolini@archlinux.org>
# Contributor:  Bartłomiej Piotrowski <bpiotrowski@archlinux.org>
# Contributor: Allan McRae <allan@archlinux.org>
# Contributor: judd <jvinet@zeroflux.org>

# ALARM: Kevin Mihelich <kevin@archlinuxarm.org>
#  - for some reason, SHLIB_LIBS isn't populating through for our builds.
#  - fixed with a sed.

pkgname=readline
_basever=8.2
_patchlevel=013
pkgver=${_basever}.${_patchlevel}
pkgrel=1
pkgdesc='GNU readline library'
arch=('x86_64')
url='https://tiswww.case.edu/php/chet/readline/rltop.html'
license=('GPL-3.0-only')
backup=('etc/inputrc')
depends=('glibc' 'ncurses' 'libncursesw.so')
provides=('libhistory.so' 'libreadline.so')
options=('!emptydirs')
source=(https://ftp.gnu.org/gnu/readline/readline-$_basever.tar.gz{,.sig}
        inputrc)
b2sums=('7974322b9c092a756a79e537df08e8532f8e0fcb598f77732e28287c33ebec9e9837ed88b43334c310892d56a871b423903f0f564def2fbe700a1004f2ae7b18'
        'SKIP'
        '50db43bff430f282175aba4c4259e0b2222bc7e342fbe47b9dcce0172458472e72aebb9852eeafa4d10d4e89f2e1e8bb83b6b5dfc68eeababe699d4b5eae80f7'
        '2e8f3d2aa14c4c4c4f9621f80f4ffae9bbd6f46c3561e87aaf684c759ffd82e3c46843cf4c4224533b28a847d83f7b8b254813f8a306d438e9f7cb457516c4e0'
        'SKIP'
        '799f089105be9931ba13d562d4579b4d1dbdd5440b625b8fe0f1cec6438a1623bf473944cb278b937ca5ab2b98eac2f5993d6e047b96368b5fcd5649fb536d96'
        'SKIP'
        'eee048069c7e11c9d2571adde31a61a8558b3b047403225039304621464c3469eaa3bcc6a4476b8530a1e9bbac3dea9a46724b6cce135b9f1aa189065737726e'
        'SKIP'
        '1c72743e33bc0d1ffab3af4b3658e212889e3be1f65e952883e1c95ed00eda6a828022955a20cca1762b0d300c0b8f578ae22d498a4017c9658d7e3a5eb2ec92'
        'SKIP'
        'e8b2ac303802b3a70e36714b465f6651b433bc6dcb3ba2f39a753183370114fd68f3bbe206657b3ca0288184eb3973a1f005ea50a131efcccf74167c772a156e'
        'SKIP'
        '1a78e7344e2983486bea6fd398b4922ca671016a07f18334697b5c8db2216de966e9b295fc694fc619a002acc714b7da8030b95726761b0afaa6142ff82516c3'
        'SKIP'
        '5fbdfea7080a67e197305849f25390634c266a8e2363116ec5dcdc20e6cbce5af787ffdb500edb9d8111388eb6560c6749c4004eb362039813abcbe17cc3bd4e'
        'SKIP'
        '2752ece47a63ccd3b94b59db36509fff1e3ec60edae079231b57e0352e38953ebf1e16c6b8020ad07259da8a1a56fea283228caa54973be0031afd5e86932b31'
        'SKIP'
        'ecfa4f3f445b0c90d4ba1cf4116c9a773a3e5ee737966d9d9be7a7ca98f8baa852cf800e571de59cfb27b6af77e93653824f20ff2c73d4629154d9ec8010e644'
        'SKIP'
        '44ebbf93b6449d848b2dda49716b494596d207bef08cdea0d01808b5291d68714e01ef914e97652bb73e81808a91fff829c8035a097db5b3649be1e0735e3eab'
        'SKIP'
        'c0c083021ef8d3b482013846719ddbbee8e0d0dcdb694e138d193937ec1ccbf26b10cf55c84646db1b63c1ba9f6e7e47b815ab11a49a2466fa1907389e61ba4f'
        'SKIP'
        '02f5351cdeaf818d809a3d40596ef7c7d83ab397e28486aa3ec1f25b1efe8c6ca1569d0c7e7a85170d38a37a5681cf53eb8a3b435b7c010e70ebd3d346bc25ca'
        'SKIP'
        '5af309dc331cdc2fc9c47f8a53c78731885cf70ba22c135929a30c3ab0c6c0a786d1a58b607a7aca9a6522e70aa0e9dc182ad21854d87299e48bf207ee9b02e2'
        'SKIP')
validpgpkeys=('7C0135FB088AAF6C66C650B9BB5869F064EA74AB') # Chet Ramey

if [ $_patchlevel -gt 0 ]; then
    for (( _p=1; _p <= $((10#${_patchlevel})); _p++ )); do
        source=(${source[@]} https://ftp.gnu.org/gnu/readline/readline-$_basever-patches/readline${_basever//.}-$(printf "%03d" $_p){,.sig})
    done
fi

prepare() {
  cd $pkgname-$_basever
  for (( _p=1; _p <= $((10#${_patchlevel})); _p++ )); do
    msg "applying patch readline${_basever//.}-$(printf "%03d" $_p)"
    patch -p0 -i ../readline${_basever//.}-$(printf "%03d" $_p)
  done

  # remove RPATH from shared objects (FS#14366)
  sed -i 's|-Wl,-rpath,$(libdir) ||g' support/shobj-conf

  # fix getting -lncurses through to SHLIB_LIBS
  sed -i 's/SHLIB_LIBS = @SHLIB_LIBS@/SHLIB_LIBS = @SHLIB_LIBS@ -lncurses/' shlib/Makefile.in
}

build() {
  cd $pkgname-$_basever

  # build with -fPIC for x86_64 (FS#15634)
  [[ $CARCH == "x86_64" ]] && CFLAGS="$CFLAGS -fPIC"

  ./configure --prefix=/usr
  make SHLIB_LIBS=-lncurses
}

package() {
  make -C $pkgname-$_basever DESTDIR="$pkgdir" install
  install -Dm644 inputrc "$pkgdir"/etc/inputrc
}
